
/*
 *
 * Copyright (C) 2008 Mattias Blomqvist, patr-blo at dsv dot su dot se
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

using System.Collections;

namespace ClientExample.Comparers
{
    public class SearchResultListViewComparer : IComparer
    {
        public int ColumnIndex
        {
            get;
            set;
        }

        public SearchResultListViewComparer(int colIndex)
        {
            ColumnIndex = colIndex;
        }

        public int Compare(object x, object y)
        {
            return Compare(x as System.Windows.Forms.ListViewItem, y as System.Windows.Forms.ListViewItem);
        }

        public int Compare(System.Windows.Forms.ListViewItem x, System.Windows.Forms.ListViewItem y)
        {
            switch (x.ListView.Sorting)
            {
                case System.Windows.Forms.SortOrder.Descending:
                    System.Windows.Forms.ListViewItem z = y;
                    y = x;
                    x = z;
                    break;
            }

            switch (ColumnIndex)
            {
                default:
                case 0:
                    return System.Collections.Comparer.Default.Compare(x.Text, y.Text);
                case 1:
                    return System.Collections.Comparer.Default.Compare(x.SubItems[1].Tag, y.SubItems[1].Tag);
                case 2:
                    return System.Collections.Comparer.Default.Compare(x.SubItems[2].Tag, y.SubItems[2].Tag);
                case 3:
                    return System.Collections.Comparer.Default.Compare(x.SubItems[3].Tag, y.SubItems[3].Tag);
            }
        }
    }
}
